﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using CRMMis.BLL;

public partial class CRM_Manager_ERP_CargoLeadEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState_ObjID = string.IsNullOrEmpty(Request.QueryString["ObjId"]) ? "Add" : Request.QueryString["ObjId"];
            ViewState_DetailId = "0";
            StoreBind();
            InitData();
        }
    }

    protected void StoreBind()
    {
        DataTable dtType = ERP_Type.GetList("1=1", "Type_ID ASC").Tables[0];
        DataTable dtUse = ERP_Use.GetList("1=1", "Use_ID ASC").Tables[0];
        DataTable dtCustom = ERP_Customer.GetList("1=1").Tables[0];
        Store_Type.DataSource = dtType;
        Store_Type.DataBind();
        Store_Use.DataSource = dtUse;
        Store_Use.DataBind();
        Store1.DataSource = dtCustom;
        Store1.DataBind();
        dtType.Dispose();
        dtUse.Dispose();
        dtCustom.Dispose();
    }


    [DirectMethod]
    public void SetCombCurrentnameSelect(string CargoID)
    {
        ViewState_ObjID = CargoID;
        DataTable dt = CRMMis.BLL.ERP_CusCargo.GetList(" CargoID=" + ViewState_ObjID).Tables[0];
        SetContainer(CargoPanel, dt);
    }

    private void InitData()
    {
        if (ViewState_ObjID != "Add")
        {
            DataTable dt = CRMMis.BLL.ERP_CusCargo.GetList(" CargoID=" + ViewState_ObjID).Tables[0];
            SetContainer(CargoPanel, dt);
            if (dt.Rows[0]["State"].Equals(0))
            {
                extBtn_Save.Disabled = true;
                extBtn_Save.Hidden = true;
                extBtn_Submit.Disabled = false;
                extBtn_Submit.Hidden = false;
            }
            if (dt.Rows[0]["State"].Equals(1))
            {
                if (dt.Rows[0]["Detail_State"].Equals(0))
                {
                    extBtn_Save.Disabled = true;
                    extBtn_Save.Hidden = true;
                    extBtn_Submit.Disabled = false;
                    extBtn_Submit.Hidden = false;
                }
                if (dt.Rows[0]["Detail_State"].Equals(1))
                {
                    extBtn_Save.Disabled = true;
                    extBtn_Save.Hidden = true;
                    extBtn_Submit.Disabled = true;
                    extBtn_Submit.Hidden = true;
                }
            }
        }
        else
        {
            extBtn_Save.Disabled = false;
            extBtn_Save.Hidden = false;
            extBtn_Submit.Disabled = true;
            extBtn_Submit.Hidden = true;
        }
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        BindGridData(e);
    }

    private void BindGridData(StoreRefreshDataEventArgs e)
    {
        int recordcount = ERP_CusCargo.GetRecorderCount(GetMatQryString());
        int PageSize = extcomb_CargoName.PageSize;
        int CurPage = e.Start / PageSize + 1;
        e.Total = recordcount;
        DataTable tmpDT = ERP_CusCargo.GetPageList(PageSize, CurPage, GetMatQryString(), "CargoName,Spec,Area,Version,ProduceName").Tables[0];
        store_CargoName.DataSource = tmpDT;
        store_CargoName.DataBind();
    }

    private string GetMatQryString()
    {
        string StrSql = "1=1 and  STATE=1 ";
        if (!string.IsNullOrEmpty(extcomb_CargoName.SelectedItem.Text.Trim()))
            StrSql += "  and  ( CargoName like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%'  or Spell like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%' or ByName like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%' or CargoCode like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%')";
        return StrSql;
    }

    [DirectMethod]
    public void SaveData(string type)
    {
        try
        {
            int old_DetailId = 0;
            CRMMis.Model.ERP_CusCargo oModel = new CRMMis.Model.ERP_CusCargo();
            CRMMis.Model.ERP_CusCargo_Detail dModel = new CRMMis.Model.ERP_CusCargo_Detail();
            string sWhere = "CustomId='" + FilterSpecChar(extcomb_CustomId.SelectedItem.Value)+"' and CargoName = '" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text.Trim()) + "' and Spec='" + FilterSpecChar(exttxtf_Spec.Text.Trim()) + "' and Area='" + FilterSpecChar(exttxtf_Area.Text.Trim()) + "' and Version='" + FilterSpecChar(exttxtf_Version.Text.Trim()) + "' and ProduceName='" + FilterSpecChar(exttxtf_ProduceName.Text.Trim()) + "'";
            DataTable dtM = ERP_CusCargo.GetList(sWhere).Tables[0];
            if (dtM.Rows.Count > 0)
            {
                if (type == "add")
                {
                    ViewState_ObjID = dtM.Rows[0]["CargoId"].ToString();
                    type = "addD";
                }
                if (ViewState_ObjID != "Add")
                {
                    oModel = ERP_CusCargo.GetModel(int.Parse(ViewState_ObjID));
                    sWhere = " CustomId='" + FilterSpecChar(extcomb_CustomId.SelectedItem.Value) + "' and  CargoId <>" + oModel.CargoId + " and  CargoName = '" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text.Trim()) + "' and Spec='" + FilterSpecChar(exttxtf_Spec.Text.Trim()) + "' and Area='" + FilterSpecChar(exttxtf_Area.Text.Trim()) + "' and Version='" + FilterSpecChar(exttxtf_Version.Text.Trim()) + "' and ProduceName='" + FilterSpecChar(exttxtf_ProduceName.Text.Trim()) + "'";
                    if (ERP_CusCargo.GetRecorderCount(sWhere) > 0)
                        throw new Exception("物品已存在！");
                    DataTable dt = ERP_CusCargo_Detail.GetList("CargoId='" + oModel.CargoId + "' and State=1", "DetailId DESC").Tables[0];
                    if (dt.Rows.Count > 0)
                        old_DetailId = int.Parse(dt.Rows[0]["DetailId"].ToString());
                    DataTable dtd = ERP_CusCargo_Detail.GetList("CargoId='" + oModel.CargoId + "' and State=0", "DetailId DESC").Tables[0];
                    if (dtd.Rows.Count > 0)
                        dModel = ERP_CusCargo_Detail.GetModel(int.Parse(dtd.Rows[0]["DetailId"].ToString()));
                }
            }
            else
            {
                ViewState_ObjID = "Add";
                dModel.DetailId = ERP_CusCargo_Detail.GetNewID();
            }
            oModel.CargoName = extcomb_CargoName.SelectedItem.Text.Trim().ToString();
            oModel.Spell = exttxtf_Spell.Text.Trim();
            oModel.ByName = exttxtf_ByName.Text.Trim();
            oModel.CargoCode = exttxtf_CargoCode.Text.Trim();
            oModel.Spec = exttxtf_Spec.Text.Trim();
            if (extcomb_TypeId.SelectedItem.Text != "")
                oModel.TypeId = int.Parse(extcomb_TypeId.SelectedItem.Value);
            if (extcomb_UseID.SelectedItem.Text != "")
                oModel.UseId = int.Parse(extcomb_UseID.SelectedItem.Value);
            oModel.Area = exttxtf_Area.Text.Trim();
            oModel.Version = exttxtf_Version.Text.Trim().ToString();
            oModel.ProduceName = exttxtf_ProduceName.Text.Trim().ToString();
            oModel.Unit = exttxtf_Unit.Text.Trim();
            oModel.IsImported = extradio_ii1.Checked ? 0 : 1;
            oModel.Pic = "";
            oModel.InUser = BS_LoginUserID;
            oModel.InDate = System.DateTime.Now.ToString();
            oModel.CustomId = int.Parse(extcomb_CustomId.SelectedItem.Value);

            dModel.Supplyprice = Math.Round(decimal.Parse(exttxtf_Supplyprice.Text.Trim()), 4);
            if (type == "add")
            {
                oModel.State = 0;
                dModel.State = 0;
                dModel.InUser = BS_LoginUserID;
                dModel.InDate = oModel.InDate;
            }
            if (type == "addD")
            {
                dModel.State = 0;
            }
            if (type == "update")
            {
                dModel.State = 0;
            }
            if (type == "submit")
            {
                oModel.State = 1;
                dModel.State = 1;
            }
            string err = ERP_CusCargo.Update(oModel, dModel, old_DetailId, type);
            if (err != "")
                throw new Exception(err);
            string handler = "";
            if (type == "submit")
                handler = "closeCurrTab();";
            X.Msg.Alert("系统提示", "操作成功！", handler).Show();
        }
        catch (Exception ex)
        {
            var err = ex.Message;
            X.Msg.Alert("系统提示", err).Show();
        }
        finally
        {
            GC.Collect();
        }
    }
}